<!DOCTYPE html>
<html lang="en" class="x-admin-sm">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>编辑影片信息</title>
    <link rel="stylesheet" href="./assets/layui/css/layui.css" media="all" />
    <script type="text/javascript" src="./assets/layui/layui.js"></script>
</head>
<!--作者联系 QQ 1420527913   毕设成品下载网址  codebag.cn-->
<body class="childrenBody">
    <div id="app">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>编辑影片信息</legend>
        </fieldset>
        <div class="layui-form" style="width: 80%;">
            <div class="layui-form-item"><label class="layui-form-label">电影名称</label>
                <div class="layui-input-block"><input type="text" class="layui-input" id="goodsname" v-model="goodsname"
                        placeholder="请输入电影名称" />
                </div>
            </div>
            <div class="layui-form-item"><label class="layui-form-label">影片类型</label>
                <div class="layui-input-block">
                    <select name="cateid" id="cateid" title="影片类型" id="cateid" >
                    </select>
                </div>
            </div>
            <div class="layui-form-item"><label class="layui-form-label">图片</label>
                <div class="layui-input-block">
                    <form id="head-img" enctype="multipart/form-data">
                        <input type="file" name="image" id="image" placeholder="请选择图片" />
                        <button type="button" class="layui-btn" @click="upload()"><i class="layui-icon layui-icon-upload"></i> 上传</button>
                    </form>
                </div>
            </div>
            <div class="layui-form-item"><label class="layui-form-label">价格</label>
                <div class="layui-input-block"><input type="text" class="layui-input" id="price" v-model="price"
                        placeholder="请输入价格" />
                </div>
            </div>
            <div class="layui-form-item"><label class="layui-form-label">商品介绍</label>
                <div class="layui-input-block">
                   <textarea class="layui-input" id="contents" v-model="contents" style="height: 220px;" placeholder="请输入商品介绍"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" @click.prevent="update()">提交保存</button>
                    <button class="layui-btn" @click.prevent="reset()">取消重置</button>
                </div>
            </div>
        </div>
    </div>


    <script type="text/javascript" src="./assets/js/axios.min.js"></script>
    <script type="text/javascript" src="./assets/js/vue.js"></script>
    <script type="text/javascript" src="./assets/js/jquery.min.js"></script>
    <script type="text/javascript">
      const vue = new Vue({
			el: '#app',// 绑定作用域
			data: { //定义数据用于vue的双向绑定
				goodsid: '',
				goodsname: '', // 电影名称
				cateid: '', // 影片类型
				url: '', // 图片
				price: '', // 价格
				contents: '', // 商品介绍
			},
			methods: {
				getUrlParam: function () {
					var name, value;
					var str = location.href; // 取得整个地址栏
					var num = str.indexOf("?"); // 定位参数起始位置
					str = str.substr(num + 1); // 取得所有参数 
					var arr = str.split("&"); // 各个参数放到数组里
					for (var i = 0; i < arr.length; i++) {
						num = arr[i].indexOf("=");
						if (num > 0) {
							name = arr[i].substring(0, num);
							value = arr[i].substr(num + 1);
							this.goodsid = value; // 确认主键参数
						}
					}
				},
				loadPage: function () {
					let _this = this;
					axios.get('http://localhost:8080/vue_gxhfztjxt/goods/getGoodsById.action?id=' + _this.goodsid).then(result => {
						_this.goodsname= result.data.goodsname;
						_this.cateid= result.data.cateid;
						_this.url= result.data.image;
						_this.price= result.data.price;
						_this.contents= result.data.contents;
					axios.get('http://localhost:8080/vue_gxhfztjxt/cate/getAllCate.action').then(result => {
						// _this.cate = result.data;
                      var myOptions = '';
                      for (var i = 0; i < result.data.length; i++) {
                          if (_this.cateid == result.data[i].cateid) {
                              myOptions += '<option value="' + result.data[i].cateid + '" selected >' + result.data[i].catename + '</option>';
                          } else {
                                    myOptions += '<option value="' + result.data[i].cateid + '" >' + result.data[i].catename + '</option>';
                          }
                       }
                       $("#cateid").empty();
                       $("#cateid").html(myOptions);
					});
					});
				},
				reset: function () {
					this.loadPage();
				},
				upload: function () {
					let _this = this;
					var formData = new FormData($('#head-img')[0]);
					axios.post('http://localhost:8080/vue_gxhfztjxt/ajaxupload/upload.action', formData).then(result => {
						_this.url = result.data.url;
						alert("上传成功");
					});
				},
				update: function () {
					let _this = this;
					var data = {
						goodsid: _this.goodsid,
						goodsname: _this.goodsname,
						cateid: $('#cateid').val(),
						image: _this.url,
						price: _this.price,
						contents: _this.contents,
					};
					axios.post('http://localhost:8080/vue_gxhfztjxt/goods/updateGoods.action', data).then(result => {
						if (result.data.code > 0) {
							var index = parent.layer.getFrameIndex(window.name);
							parent.location.reload();
							parent.layer.close(index);
						} else {
							alert('修改失败');
						}
					});
				},
			},

			created() { // 生命周期相关的函数
              layui.use(['element', 'layer', 'form'], function() {
                var $ = layui.jquery; // jquery
              });
				this.getUrlParam();
				this.loadPage();
				let _this = this;
			},
		})
	</script>

</body>
<!--作者联系 QQ 1420527913   毕设成品下载网址  codebag.cn-->
</html>


